/**
* Copyright (c) 2004-2013 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

package it.ciano.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Arrays;

/**
 * The class extends HashMap and implements a counter of keys.
 * The hash key is a set name (a String object),
 * and the value is the number of occurrences of the key.
 *
 * @author Luciano Xumerle
 * @version 0.0.1
 */
final public class CCounter extends HashMap
{
    private int total;
    static final long serialVersionUID = -4648601388506326778L;

    /**
     * Constructs an empty CCounter.
     */
    public CCounter()
    {
        super();
        total = 0;
    }

    /**
     * Returns the total number of occurrences in the set.
     *
     * @return the total number of occurrences in the set.
     */
    public int getTotal()
    {
        return total;
    }

    /**
     * Sets the total number of object in the set.
     *
     * @param i the total number of object in the set.
     */
    public void setTotal ( int i )
    {
        total=i;
    }

    /**
     * Returns the sorted list of the keys stored in the CCounter.
     *
     * @return the sorted list.
     */
    public String[] getKeys()
    {
        String[] res = new String[ this.size() ];
        int i = 0;
        for ( Iterator it = this.entrySet().iterator(); it.hasNext(); )
        {
            Map.Entry eee = ( Map.Entry ) it.next();
            res[ i ] = ( ( String ) eee.getKey() );
            i++;
        }
        Arrays.sort ( res );
        return res;
    }

    /**
     * Returns the number of occurrences of the key.
     *
     * @param key the set name.
     * @return the number of occurrences of the key.
     */
    public int getOccurrences ( String key )
    {
        if ( super.containsKey ( key ) )
            return ( ( Integer ) super.get ( key ) ).intValue();
        return 0;
    }

    /**
     * Sets the number of occurrences of the key.
     *
     * @param key the set name.
     */
    public void setOccurrences ( String key, int i )
    {
        super.put ( key, new Integer ( i ) );
    }

    /**
     * Returns the frequency of a set.
     *
     * @param key the set name.
     * @return the frequency.
     */
    public float getFrequencies ( String key )
    {
        return ( float ) getOccurrences ( key ) / total;
    }

    /**
     * Increment by one the counter of the key.
     *
     * @param key the set name.
     */
    public void inc ( String key )
    {
        int current = getOccurrences ( key );
        super.put ( key, new Integer ( current + 1 ) );
        total++;
    }

    /**
     * Decrement by one the counter of the key.
     *
     * @param key the set name.
     */
    public void dec ( String key )
    {
        int current = getOccurrences ( key );
        if ( current > 0 )
        {
            super.put ( key, new Integer ( current - 1 ) );
            total--;
        }
    }

} // end class

